package me.zhengjie.modules.website.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import me.zhengjie.modules.website.domain.ArchiveInformation;
import me.zhengjie.modules.website.service.ArchiveInformationService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import me.zhengjie.modules.website.domain.PageResult;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author system
 * @since 2024-11-05 23:38:48
 */
@RestController
@RequestMapping("/api/archiveInformation")
@Api(tags = " 网页-档案管理")
public class ArchiveInformationController {

    @Autowired
    private ArchiveInformationService archiveInformationService;

    /**
     * 展示平台-分页查询
     *
     * @param params    查询条件
     * @param startDate 起始时间（可选）
     * @param endDate   截止时间（可选）
     * @param pageable  分页信息
     * @return 分页结果
     */
    @ApiOperation(value = "展示平台-分页查询", notes = "展示平台-分页查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/website/findPage")
    public ResponseEntity<PageResult<ArchiveInformation>> findPage(
            @RequestBody ArchiveInformation params,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
            Pageable pageable) {

        PageResult<ArchiveInformation> result = archiveInformationService.findPage(params, startDate, endDate, pageable);
        return ResponseEntity.ok(result);
    }


    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/findPage")
    public ResponseEntity<PageResult<ArchiveInformation>> findPageByEndBack(
            @RequestBody ArchiveInformation params,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
            Pageable pageable) {

        PageResult<ArchiveInformation> result = archiveInformationService.findPage(params, startDate, endDate, pageable);
        return ResponseEntity.ok(result);
    }


    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/findList")
    public ResponseEntity<List<ArchiveInformation>> findList(@RequestBody ArchiveInformation params) {
        List<ArchiveInformation> result = archiveInformationService.findList(params);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping("findById/{id}")
    public ResponseEntity<ArchiveInformation> findById(@PathVariable("id") Long id) {
        ArchiveInformation archiveInformation = archiveInformationService.findById(id);
        return ResponseEntity.ok(archiveInformation);
    }

    /**
     * 新增
     *
     * @param archiveInformation
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/insert")
    public ResponseEntity<ArchiveInformation> insert(@Validated @RequestBody ArchiveInformation archiveInformation) {
        ArchiveInformation result = archiveInformationService.insert(archiveInformation);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改
     *
     * @param archiveInformation
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PutMapping("/update")
    public ResponseEntity<ArchiveInformation> update(@Validated @RequestBody ArchiveInformation archiveInformation) {
        ArchiveInformation result = archiveInformationService.update(archiveInformation);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        archiveInformationService.delete(id);
        return ResponseEntity.ok(null);
    }

    /**
     *
     * 批量导入
     * @param file
     * @return
     */
    @PostMapping("/import")
    @ApiOperation("批量导入档案信息")
    public ResponseEntity<Object> importArchiveInformation(@RequestParam("file") MultipartFile file) {
        try {
            List<ArchiveInformation> importedData = archiveInformationService.importExcel(file);
            return ResponseEntity.ok(importedData);
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
    }
}